/*
 * 查询
 */
Ext.onReady(function() {

	Ext.QuickTips.init();
	var v_start=0, v_limit=20;
	
	var sysCompanyStore = new Ext.data.JsonStore({
	    xtype:'jsonstore',
		url: i1510.contextPath + '/admin/sysCompany/listExt.do?docStatus=10',
	    root: 'root',
	    totalProperty: 'total',
	    fields: ['company','docSubject'],
		autoLoad:true
	});
	
	var sysRoleStore = new Ext.data.JsonStore({
	    xtype:'jsonstore',
		url: i1510.contextPath + '/admin/sysRole/listExt.do?docStatus=10',
	    root: 'root',
	    totalProperty: 'total',
	    fields: ['docId','docSubject'],
		autoLoad:true
	});
	
    var sysDataSexStore = new Ext.data.JsonStore({
	    xtype:'jsonstore',
		url: i1510.contextPath + '/admin/sysData/listExt.do?docStatus=10&fdParentCode=SEX',
	    root: 'root',
	    totalProperty: 'total',
	    fields: ['fdCode','docSubject'],
		autoLoad:true
	});
    
    
	// sysUser 表单 Start
	//--add表单sysOrg下拉数据
	//商品类别树窗口
    var add_sysOrgTreeWindow = new Ext.Window({
		width:200,
		height:300,
		closeAction:'hide',
		layout : 'fit',
		buttonAlign : 'center',
		items : [{
			xtype:'treepanel',
			useArrows: true,
	        autoScroll:true,
	        enableDD:false,
	        containerScroll: true,
	        dataUrl: i1510.contextPath + '/admin/sysOrg/findTree.do?docStatus=10',
	        root: {
	            nodeType: 'async',
	            text: '所属组织',
	            draggable: false,
	            id: '0000'
	        },
	        listeners:{
	        	click:function(n){
	        		if(n.id=="0000"){
	        			add_sysOrgTreeWindow.buttons[0].setDisabled(true);
	        		}else{
	        			add_sysOrgTreeWindow.buttons[0].setDisabled(false);
	        		}
	        	},
	        	dblclick:function(n){
	        		if(n.id=="0000"){
	        			add_sysOrgTreeWindow.buttons[0].setDisabled(true);
	        		}else{
	        			add_sysOrgTreeWindow.buttons[0].setDisabled(false);
			        	add_sysOrgTreeWindow.hide();
						addForm.getForm().findField("orgId").setValue(n.id);
						addForm.getForm().findField("orgName").setValue(n.text);
	        		}
	        	}
	        }
		}],
		listeners:{
			beforeshow:function(){
				var xy = Ext.getCmp("orgName").getPosition();
				add_sysOrgTreeWindow.setPosition(xy[0],xy[1]+25);
			},
        	show:function(){
        		this.items.get(0).getRootNode().expand();
        	}
        },
		buttons : [{
			width:60,
			text : '选择',
			disabled : true,
			handler : function() {
				add_sysOrgTreeWindow.hide();
				addForm.getForm().findField("orgId").setValue(v_lbid);
				addForm.getForm().findField("orgName").setValue(v_lbname);
			}
		}, {
			width:60,
			text : '取消',
			handler : function() {
				add_sysOrgTreeWindow.hide();
			}
		}]
	});
	
	//add表单
    var addForm = new Ext.FormPanel({
		layout : 'form',
		frame:true,
		labelWidth:60,
		border : false,
		padding : '15 10 0 8',
		defaults : {
			anchor : '100%'
		},
		items:[{
			layout:'column',
			items:[{
				columnWidth:.5,
				layout:'form',
				defaults:{
					anchor : '95%'
				},
				items:[{
					layout:'column',
					items:[{
						layout:'form',
						width : 218,
						items:[{
							id:'orgName',
							width : 145,
							xtype : 'textfield',
							name:'orgName',
							fieldLabel:'所属组织',
							allowBlank : true,
							maxLength :50,
							enableKeyEvents:true,
							listeners:{
								focus:function(){
									add_sysOrgTreeWindow.show();
								},
								blur:function(){
									addForm.getForm().clearInvalid();
								}
							}
						}]
					},{
						width : 20,
						height : 20,
						xtype:'button',
						iconCls : 'btn-list',
						handler:function(){
							add_sysOrgTreeWindow.show();
						}
					}]
				},{
					xtype : 'textfield',
					name:'name',
					fieldLabel:'注册名称',
					allowBlank : false,
					maxLength :20
				},{
					xtype : 'textfield',
					name:'email',
					fieldLabel:'电子邮件',
					maxLength :20
				},{
					xtype : 'textfield',
					name:'nickName',
					fieldLabel:'昵&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;称',
					maxLength :10
				},{
					xtype:'combo',
					hiddenName:'sex',//如果不设置就会提交displayField的值
					name:'sex',
					fieldLabel:'性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别',
					mode: 'local',
					maxLength :10,
					triggerAction: 'all',
					valueField :'fdCode',
					displayField: 'docSubject',
					emptyText: '请选择',
					editable : false,
					store : sysDataSexStore
				}]
			},{
				columnWidth:.5,
				layout:'form',
				defaults:{
					anchor : '95%'
				},
				items:[{
					xtype:'combo',
					hiddenName:'company',//如果不设置就会提交displayField的值
					name:'company',
					fieldLabel:'公&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;司',
					mode: 'local',
					maxLength :20,
					triggerAction: 'all',
					valueField :'company',
					displayField: 'docSubject',
					emptyText: '请选择',
					editable : false,
					store : sysCompanyStore
				},{
					xtype : 'textfield',
					name:'pw',
					fieldLabel:'密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码',
					style:"background:#F6F6F6",
					maxLength :10
				},{
					xtype : 'numberfield',
					name:'vitNum',
					fieldLabel:'活跃程度',
					maxLength :10
				},{
					xtype:'combo',
					hiddenName:'fdRoleId',//如果不设置就会提交displayField的值
					name:'fdRoleId',
					fieldLabel:'角&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;色',
					mode: 'local',
					maxLength :10,
					triggerAction: 'all',
					valueField :'docId',
					displayField: 'docSubject',
					emptyText: '请选择',
					editable : false,
					store : sysRoleStore
				}]
			}]
		},{
			xtype : 'textfield',
			name:'oneWord',
			fieldLabel:'签&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名',
			maxLength :50
		},{
			xtype:'textarea',
			name:'project',
			fieldLabel:'项&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;目',
			height:100,
			maxLength :200
		},{
			xtype : 'hidden',
		    name : 'docId'
		},{
			xtype : 'hidden',
		    name : 'orgId'
		}]
	});
    var fn_submit = function(next){

		if (addForm.getForm().isValid()) {
			addForm.getForm().submit({
			    url: i1510.contextPath+'/admin/sysUser/save.do',
				success : function(form, rtn) {
					
					if(next){
				   		addForm.getForm().reset();
					}else{
						addWin.hide();
					}
				   	//刷新list
				   	listStore.reload();
				   
				},
				failure : function(form, rtn) {
					Ext.Msg.alert('信息提示','提交失败.'+rtn.result.message);
				},
				waitTitle : '提交',
				waitMsg : '正在保存数据，稍后...'
			});
		}
	}
	//增加窗口
    var addWin = new Ext.Window({
		title : '新增/修改',
		width:550,
		height:370,
		closeAction:'hide',
		modal : true,
		border:false,
		resizable : false,
		layout : 'fit',
		buttonAlign : 'center',
		items : [addForm],
		buttons : [{
			text : '保存并新增',
			handler : function() {
				fn_submit(true);
			}
		},{
			text : '保存',
			handler : function() {
				fn_submit(false);
			}
		}, {
			text : '取消',
			handler : function() {
				addWin.hide();
			}
		}]
	});
	
	
	// sysUser 表单 End

	var sysOrgTree_docId = "0000", sysOrgTree_docSubject = "所属组织";

	// 查询sysOrg树窗口
	var sysOrgTreeWindow = new Ext.Window({
		iconCls : 'menu-53',
		title : '组织树',
		width : 240,
		height : 260,
		closeAction : "hide",
		layout : "fit",
		buttonAlign : "center",
		items : [ {
			xtype : "treepanel",
			useArrows : true,
			autoScroll : true,
			enableDD : false,
			containerScroll : true,
			dataUrl : i1510.contextPath
					+ '/admin/sysOrg/findTree.do?docStatus=10',
			root : {
				nodeType : "async",
				text : "所属组织",
				select : true,
				draggable : false,
				id : "0000"
			},
			listeners : {
				load : function() {
					this.getSelectionModel().select(this.root);
				},
				click : function(n) {
	        		if(n.id=="0000"){
	        			sysOrgTreeWindow.buttons[0].setDisabled(true);
	        		}else{
	        			sysOrgTreeWindow.buttons[0].setDisabled(false);
	        		}
					sysOrgTree_docId = n.id;
					sysOrgTree_docSubject = n.text;
				},
				dblclick : function(n) {
					sysOrgTree_docId = n.id;
					sysOrgTree_docSubject = n.text;
					sysOrgTreeWindow.hide();
					searchForm.getForm().findField("sorgId").setValue(
							sysOrgTree_docId);
					searchForm.getForm().findField("sorgName").setValue(
							sysOrgTree_docSubject);
				}

			}
		} ],
		listeners : {
			beforeshow : function() {
				var xy = Ext.getCmp("sorgName").getPosition();
				sysOrgTreeWindow.setPosition(xy[0], xy[1] + 25);
			},
			show : function() {
				this.items.get(0).getRootNode().expand();
			}
		},
		buttons : [
				{
					width : 60,
					text : "选择",
					handler : function() {
						sysOrgTreeWindow.hide();
						searchForm.getForm().findField("sorgId").setValue(
								sysOrgTree_docId);
						searchForm.getForm().findField("sorgName").setValue(
								sysOrgTree_docSubject);
					}
				}, {
					width : 60,
					text : "取消选择",
					handler : function() {
						sysOrgTreeWindow.hide();
						searchForm.getForm().findField("sorgId").setValue("");
						searchForm.getForm().findField("sorgName").setValue("");
					}
				}, {
					width : 60,
					text : "取消",
					handler : function() {
						sysOrgTreeWindow.hide();
					}
				} ]
	});

	// 查询表单
	var searchForm = new Ext.FormPanel({
		region : "north",
		height : 80,
		border : false,
		layout : "form",
		padding : "5 20 0 20",
		items : [ {
			xtype : "fieldset",
			title : "查询设置",
			padding : "0 20 0 15",
			items : [ {
				layout : "column",
				defaults : {
					xtype : "container",
					autoEl : "div",
					labelAlign : "right",
					layout : "form"
				},
				items : [ {
					width : 190,
					labelWidth : 60,
					items : [ {
						width : 100,
						id : "sorgName",
						xtype : "textfield",
						name : "sorgName",
						fieldLabel : "所属组织",
						value : "",
						maxLength : 50,
						enableKeyEvents : true,
						listeners : {
							focus : function() {
								sysOrgTreeWindow.show();
							}
						}
					} ]
				}, {
					width : 310,
					items : [ {
						width : 180,
						labelWidth : 100,
						xtype : "textfield",
						name : "sname",
						fieldLabel : "注册账号"
					} ]
				}, {
					width : 250,
					items : [ {
						width : 75,
						xtype : "button",
						iconCls : "btn-list",
						text : "查询",
						handler : function() {
							var f = searchForm.getForm();
							if (f.isValid()) {
								listStore.load({
									params : f.getValues()
								});
							}
						}
					} ]
				} ]
			}, {
				xtype : 'hidden',
				name : 'sorgId',
				value : ''
			} ]
		} ]
	});
	// 数据映射
	var mapObj = [ {
		name : 'docId',
		type : 'string'
	}, {
		name : 'name',
		type : 'string'
	}, {
		name : 'nickName',
		type : 'string'
	}, {
		name : 'orgName',
		type : 'string'
	}, {
		name : 'email',
		type : 'string'
	}, {
		name : 'docCreateTime',
		type : 'string'
	}, {
		name : 'docStatus',
		type : 'string'
	}, {
		name : 'company',
		type : 'string'
	}, {
		name : 'sex',
		type : 'string'
	},{
		name : 'fdRoleId',
		type : 'string'
	}, {
		name : 'orgId',
		type : 'string'
	}, {
		name : 'vitNum',
		type : 'string'
	}, {
		name : 'oneWord',
		type : 'string'
	}, {
		name : 'project',
		type : 'string'
	}];

	// 列表数据
	var listStore = new Ext.data.JsonStore({
		url : i1510.contextPath + '/admin/sysUser/listExt.do',
		root : 'root',
		totalProperty : 'total',
		fields : mapObj
	});
	// 在listStore执行前添加查询参数
	listStore.on('beforeload', function() {
		var f = searchForm.getForm();
		if (f.isValid()) {
			listStore.baseParams = f.getValues();
		}

	});
	//list 新增
	function fn_add(){
		addWin.show();
		addForm.getForm().reset();
		addForm.getForm().findField("name").setDisabled(false);
		addForm.getForm().findField("pw").setDisabled(true);
	}
	//list 更新
	function fn_update(){
		var record = listGrid.getSelectionModel().getSelected();
		if (!record) {
			Ext.Msg.alert('信息提示', '请选择要修改的数据');
		} else {
			addWin.show();
			addForm.getForm().loadRecord(record);
			addForm.getForm().findField("name").setDisabled(true);
			addForm.getForm().findField("pw").setDisabled(true);
				
		}
	}
	//list 删除
	function fn_del(varStatus){
		var record = listGrid.getSelectionModel().getSelected();
		if (!record) {
			Ext.Msg.alert('信息提示', '请选择要修改的数据');
		} else {
			Ext.MessageBox.confirm('修改提示', '是否修改该用户状态？', function(c) {
				if (c == 'yes') {
					Ext.Ajax.request({
						url : i1510.contextPath + '/admin/sysUser/updateStatus.do',
						params : {
							docId : record.get("docId"),
							docStatus:varStatus
						},
						success : function() {
							listStore.reload();
						}
					});
				}
			});
		}
	}
	// 列表
	var listGrid = new Ext.grid.GridPanel({
		id : "listGrid",
		store : listStore,
		cm : new Ext.grid.ColumnModel({
			defaults : {
				menuDisabled : true
			},// 禁止表头菜单
			columns : [ {
				header : 'ID',
				sortable : true,
				dataIndex : 'docId',
				hidden : true
			}, {
				header : '注册账号',
				sortable : true,
				dataIndex : 'name'
			}, {
				header : '昵称',
				sortable : true,
				dataIndex : 'nickName'
			}, {
				header : '所属组织',
				sortable : true,
				dataIndex : 'orgName'
			}, {
				header : '邮件地址',
				sortable : true,
				dataIndex : 'email'
			}, {
				header : '创建时间',
				sortable : true,
				dataIndex : 'docCreateTime'
			}, {
				header : '状态',
				sortable : true,
				dataIndex : 'docStatus',
				renderer:fn_docStatusExt
			}]
		}),
		stripeRows : true, // 行分隔符
		columnLines : true, // 列分隔符
		height : 150,
		margins : "10 20 5 20",
		style : "border:1px solid",
		region : "center",
		listeners : {
			render : function() {
				var f = searchForm.getForm();
				if (f.isValid()) {
					listStore.load({
						params : f.getValues()
					});
				}
				// listStore.load({params:searchForm.getForm().getValues()});
			},
        	rowdblclick:function(){
        		fn_update();
        	}
		},
		iconCls : 'menu-62',

		tbar : [ {
			text : '增加',
			iconCls : 'btn-add',
			handler : function() {
				fn_add();
			}
		}, '-', {
			text : '修改',
			iconCls : 'btn-edit',
			handler : function() {
				fn_update();
			}
		}, '-', {
			text : '激活',
			iconCls : 'clock',
			handler : function() {
				fn_del("10");
			}
		}, '-', {
			text : '删除',
			iconCls : 'btn-remove',
			handler : function() {
				fn_del("00");
			}
		}  ],

		bbar : new Ext.PagingToolbar({
			pageSize : v_limit,
			store : listStore,
			displayInfo : true
		})
	});

	// 布局
	new Ext.Viewport({
		layout : "fit",
		items : [ {
			frame : true,
			title : "人员信息查询",
			iconCls : 'menu-62',
			layout : "border",
			items : [ searchForm, listGrid ]
		} ]
	});

});